package com.ywk.base.toolClass.regex;

import org.springframework.http.converter.StringHttpMessageConverter;
import org.springframework.web.client.RestTemplate;

import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 正则表达式语义：https://www.runoob.com/java/java-regular-expressions.html
 * 在线测试正则表达式工具：https://regex101.com/
 *
 * @Author zbh
 * @Date 2023/6/28
 */
public class RegexDemo {
    public static void main(String[] args) {
        System.out.println("--------------------demo1--------------------");
        demo1();
        System.out.println("--------------------命名规则--------------------");
        nameRule();
        System.out.println("--------------------替换符号--------------------");
        replace();
        System.out.println("--------------------去除前导0--------------------");
        delPre0();
        System.out.println("--------------------ip匹配--------------------");
        ipRule();
        System.out.println("--------------------email匹配--------------------");
        emailRule();
        System.out.println("--------------------爬虫--------------------");
        htmlSpider();
    }

    private static void demo1() {
        //默认是贪婪匹配,顺序匹配（即每一组都尽量匹配多的字符，按表达式的顺序匹配）
        Pattern compile = Pattern.compile("([abc][abc])|([abc])");
//        Pattern compile = Pattern.compile("([abc])|([abc][abc])");
        Matcher matcher = compile.matcher("aaabaccd");
        while (matcher.find()) {
            System.out.println(matcher.group());
        }
    }


    /**
     * 命名规则：必须都是大写字母且以大写字母开头和结束，中间可以穿插_
     *
     */
    public static void nameRule(){
        List<String> arr = Arrays.asList("BIG_DATA_baidu_point","BIG_DATA_S", "xx_BIG_DATA_", "BIG_oad_aa","sd_FDA,P7&^","_DATA_");
        Pattern pattern = Pattern.compile("^[A-Z]+(_[A-Z]+)*$");
//        Pattern pattern = Pattern.compile("(?<tag>^[A-Z]+(_[A-Z]+)*$)");//给对应组命名
        for (int i = 0; i < arr.size(); i++) {
            Matcher matcher = pattern.matcher(arr.get(i));
//            System.out.println("tag："+matcher.group("tag"));
            while (matcher.find()) {
                //group(0)代表匹配的整串字符，group(1)代表第一个(pattern)表达式所匹配的最后一个的字符串，group(2)表第二个(pattern)表达式所匹配的最后一个的字符串...
                System.out.println(i+"-0："+matcher.group(0));
//                System.out.println(i+"-1："+matcher.group(1));
//                System.out.println("数组"+i+"-分组2："+matcher.group(2));
            }
        }

    }

    /**
     * 目标：替换小写字母前面的符号 _ 成 .
     */
    public static void replace(){

        String str="BIG_DATA_baidu_point";
        String regex="_(?=(?:[a-z]))";//找到目标符号_（即小写字母前面的_）
        Pattern pattern = Pattern.compile(regex);
        Matcher matcher = pattern.matcher(str);
        System.out.println(matcher.replaceAll("."));

        //字符串自带API原理就是上面的写法
        System.out.println(str.replaceAll(regex, "."));

        //其他API扩展
        /*for (String s : str.split(regex, 2)) {//limit 值可以换成其他值
            System.out.println(s);
        }*/
    }

    /**
     * 去除数字的前置0
     */
    private static void delPre0() {
        List<String> arr = Arrays.asList("5","016", "00f", "01","4","00","5d","000");
        Pattern pattern = Pattern.compile("(?!0)[0-9]+$");
        for (String s : arr) {
            //处理全都是0的
            Pattern compile = Pattern.compile("^0+$");
            Matcher m0 = compile.matcher(s);
            if (m0.matches()){
                System.out.println(0);
            }
            Matcher matcher = pattern.matcher(s);
            while (matcher.find()) {
                System.out.println(matcher.group());
            }
        }

    }

    /**
     * 规则：0-255,0-255,0-255,0-255
     */
    private static void ipRule() {
//        List<String> arr = Arrays.asList("192.08.1.1","192.168.1.100", "255.256.1.0","0.0.0.0","192.5..5.1.1","255.255.255.255","1.5.16.88","14.162.8.56");
        List<String> arr = Arrays.asList("192","2", "255", "256","16","08");
        /*
        * 当数字为一位时 [0-9]
        * 两位：[1-9][0-9]
        * 三位(分1开头和，2开头，25开头)：1[0-9][0-9]，2[0-4][0-9]，25[0-5]
        * */
        Pattern pattern = Pattern.compile("^([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])$");//匹配：0-255
//        Pattern pattern = Pattern.compile("^([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])(\\.([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])){3}$");
        for (int i = 0; i < arr.size(); i++) {
            Matcher matcher = pattern.matcher(arr.get(i));
            while (matcher.find()) {
                System.out.println(i+"-0："+matcher.group());
            }
        }
    }

    /**
     * 规则：
     * 1.开头和结束是字母或数字
     * 2.符号只能用._-，但不能连续
     * 3.@前后字符必须是字母或数字，不能符号
     * 4.域名是.xxx（可以多个），xxx是字母数字，可以穿插_-符号
     */
    private static void emailRule() {
        List<String> arr = Arrays.asList("ab.cd@dft.com.cn","_abc@dft.com","abc.@dft.com","a_-bc@dft.com","ab.cd_ef@dft.com.cn",
                "abc@_dft.com","abc@dft._com","abc@dft.com.zxc_s");
        Pattern compile = Pattern.compile("^[a-zA-Z0-9]+([._-][a-zA-Z0-9]+)*@[a-zA-Z0-9]+(.([a-zA-Z0-9]+[_-]?[a-zA-Z0-9]+))+$");
        for (int i = 0; i < arr.size(); i++) {
            Matcher matcher = compile.matcher(arr.get(i));
            while (matcher.find()) {
                System.out.println(i+"-0："+matcher.group());
            }
        }
    }

    /**
     * 爬虫：用正则表达式
     */
    public static void htmlSpider(){
        RestTemplate restTemplate=new RestTemplate();
        restTemplate.getMessageConverters().set(1, new StringHttpMessageConverter(StandardCharsets.UTF_8));
        String resStr = restTemplate.getForObject("http://www.weather.com.cn/weather1d/101110701.shtml", String.class);
        //正则匹配
        Pattern compile = Pattern.compile("(?:observe24h_data = )(.*})");
        Matcher matcher = compile.matcher(resStr);
        String rsp="";
        while (matcher.find()) {
            rsp = matcher.group(1);
        }
        System.out.println(rsp);
    }
}
